//
//  MoreComplaintsViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-17.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "ComplaintsViewController.h"

#define INPUT_MARK @"请输入投诉内容"

@interface ComplaintsViewController ()

@property (strong, nonatomic) UIColor *oldColor;

@end

@implementation ComplaintsViewController

- (id)init
{
    self = [super initWithNibClass:[ComplaintsViewController class]];
    if (self) {
        _dataSource = [[ComplaintsDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

#pragma 隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"餐厅投诉"];
    
    _oldColor = _contentTV.textColor;
    
    [self.shopNameTF leftMargin:5];
    [self.view addTarget:self action:@selector(viewTapped:)];
    [UIView drawBorders:@[_shopNameTF, _contentTV]];
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewTapped:(id)sender
{
    if ([self.shopNameTF resignFirstResponder] || [_contentTV resignFirstResponder]) {
        if ([_contentTV.text isEqualToString:@""]) {
            _contentTV.text = INPUT_MARK;
            _contentTV.textColor = _oldColor;
        }
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([_contentTV.text isEqualToString:INPUT_MARK]) {
        _contentTV.text = @"";
        _contentTV.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([_contentTV.text isEqualToString:@""]) {
        _contentTV.text = INPUT_MARK;
        _contentTV.textColor = _oldColor;
    }
}

- (IBAction)submitAction:(id)sender
{
    if (![self checkForm]) return;
    
    [_shopNameTF resignFirstResponder];
    [_contentTV resignFirstResponder];
    [SVProgressHUD showWithStatus:@"您的投诉正在提交中..."];
    
    NSString *shopName = _shopNameTF.text;
    NSString *content = _contentTV.text;
    NSString *url = [NSString stringWithFormat:API_COMPLAINT, shopName, content];
    
    [_dataSource submitWithURL:url];
}

- (BOOL)checkForm
{
    NSString *shopName = _shopNameTF.text;
    NSString *content = _contentTV.text;

    if ([shopName isEqualToString:S_EMPTY]) {
        [AppDelegate showMessage:@"请填写餐厅名称！"];
        return NO;
    }
    
    if ([content isEqualToString:S_EMPTY] || [content isEqualToString:INPUT_MARK]) {
        [AppDelegate showMessage:@"请填写投诉内容！"];
        return NO;
    }
    
    return YES;
}

@end

//==============================================================================
#pragma=========数据源实现========================================================
//==============================================================================

@implementation ComplaintsDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)submitWithURL:(NSString *)url
{
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    if ([[json objectForKey:@"state"] intValue] == 1) {
        [SVProgressHUD showSuccessWithStatus:@"您的投诉已经提交了！"];
        [_viewController performSelector:@selector(leftButtonClick:) withObject:nil afterDelay:1];
    }else {
        [SVProgressHUD showErrorWithStatus:@"您的投诉提交失败！"];
    }
}

@end
